//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats
{
#if DEBUG
    [ContractClass(typeof (SpreadsheetFormatContract))]
#endif
#if NET40
    [System.ComponentModel.Composition.InheritedExport]
#endif
    public abstract class  SpreadsheetFormat
    {
        /// <summary>
        /// Open a spreadsheet document from a file.
        /// </summary>
        /// <param name="fileName">The name of the file to open</param>
        /// <returns></returns>
        public virtual SpreadsheetDocument Open(string fileName)
        {
            using (Stream stream = File.Open(fileName, FileMode.Open))
            {
                return Open(stream);
            }
        }
        /// <summary>
        /// Open a spreadsheet document from a file
        /// </summary>
        /// <param name="stream">The stream to read</param>
        /// <returns></returns>
        public abstract SpreadsheetDocument Open(Stream stream);
        /// <summary>
        /// Save the spreadsheet document to file using the current format
        /// </summary>
        /// <param name="document">The document to save</param>
        /// <param name="fileName">The file name to save to</param>
        public virtual void Save (SpreadsheetDocument document, string fileName)
        {
            using (Stream stream = File.Open(fileName, FileMode.OpenOrCreate))
            {
                Save(document, stream);
            }
        }
        /// <summary>
        /// Save the spreadsheet document to a stream
        /// </summary>
        /// <param name="document">The document to save</param>
        /// <param name="stream">The stream to save to</param>
        public abstract void Save(SpreadsheetDocument document, Stream stream);
#if DEBUG
        [ContractClassFor(typeof (SpreadsheetFormat))]
        abstract class SpreadsheetFormatContract: SpreadsheetFormat
        {
            public override SpreadsheetDocument Open(Stream stream)
            {
                Contract.Requires(stream != null);
                Contract.Ensures(Contract.Result<SpreadsheetDocument>() != null);
                return default(SpreadsheetDocument);
            }
            public override SpreadsheetDocument Open(string fileName)
            {
                Contract.Assert(fileName != null);
                throw new NotSupportedException();
            }
            public override void Save(SpreadsheetDocument document, Stream stream)
            {
                Contract.Requires(document != null);
                Contract.Requires(stream != null);
            }
        }
#endif
        public abstract string ClipboardName { get; }
        public abstract IEnumerable<string> Extensions { get; }
    }
}
